package com.dnjn.mes.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.mes.biz.domain.MesRecordStatus;
import com.dnjn.mes.biz.mapper.MesRecordStatusMapper;
import com.dnjn.mes.biz.service.IMesRecordStatusService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 状态表 仅记录有变化时的状态 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-07-28
 */
@Service
public class MesRecordStatusServiceImpl extends ServiceImpl<MesRecordStatusMapper, MesRecordStatus> implements IMesRecordStatusService {

    @Override
    public boolean addStatus(String status, Long equipmentId, LocalDateTime now) {
        MesRecordStatus statusDetail = new MesRecordStatus()
                .setStatus(status)
                .setEquipmentId(equipmentId)
                .setStartDatetime(now)
                .setCurrentDatetime(now);
        return save(statusDetail);
    }

    @Override
    public MesRecordStatus lastRecordStatus(String status) {

        return lambdaQuery()
                .eq(MesRecordStatus::getStatus, status)
                .orderByDesc(MesRecordStatus::getCreateTime)
                .last("limit 1")
                .one();
    }
}
